import { defineStore } from 'pinia';
import { computed, ref } from 'vue';

function initState() {
  return {
    isCollapse: false,
    tags: [
      {
        name: 'home',
        path: '/home',
        titleKey: 'home', // 使用 titleKey 而不是 title
      },
    ],
    currentTag: null,
  };
}

export const useHomeDataStore = defineStore('homeData', () => {
  // ref 类似于 state属性
  const state = ref(initState());
  function selectMenu(val) {
    if (val.name === 'home') {
      state.value.currentTag = null;
    } else {
      let index = state.value.tags.findIndex((item) => item.name === val.name);
      if (index === -1) {
        // 添加新标签时，确保包含 titleKey
        const newTag = {
          name: val.name,
          path: val.path,
          titleKey: val.titleKey || val.name // 使用 titleKey，如果没有则使用 name
        };
        state.value.tags.push(newTag);
      }
    }
  }
  function updateMenu(tags) {
    let index = state.value.tags.findIndex((item) => item.name === tags.name);
    if (index !== -1) {
      state.value.tags.splice(index, 1); // 删除标签
    }
  }

  return {
    state, // 直接暴露整个 state
    selectMenu,
    updateMenu,
  };
});
